﻿using AnyLink.PosClient.View;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Windows;

namespace AnyLink.PosClient.ViewModel
{
    public partial class LoginViewModel : ObservableObject
    {
        readonly ILogger _logger;
        public LoginViewModel(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<LoginViewModel>();
            Username = "admin";
        }

        //[NotifyPropertyChangedFor(nameof(FullName))]
        //[NotifyCanExecuteChangedFor(nameof(GreetUser))]
        [ObservableProperty]
        //[NotifyDataErrorInfo]
        //[Required]
        //[MinLength(4)]
        public string username;
        [ObservableProperty]
        public string password;

        
        /// <summary>
        /// 登录
        /// </summary>
        [RelayCommand]
        private void Login()
        {
            _logger.LogDebug("Login");
            if (Username=="admin"&&Password=="123456")
            {
                MessageBox.Show("登录成功，正在转跳到首页！");                
                App.Current.MainWindow = App.Current.Services.GetRequiredService<MainWin>();
                App.Current.MainWindow.Show();
                App.Current.Services.GetRequiredService<LoginWin>().Close();
            }
            else
            {
                MessageBox.Show("用户名或密码错误");
            }
        }
        /// <summary>
        /// 退出程序
        /// </summary>
        [RelayCommand]
        private void Cancel()
        {
            _logger.LogDebug("退出程序");
            Environment.Exit(0);
            //WeakReferenceMessenger.Default.Send("退出程序", "AppClose");
        }
    }
}
